// eee.Sheffield.PZ.Imaging
//
// Copyright ?Ping Zou, 2007
// sg71.cherub@gmail.com

using System;
using System.Collections.Generic;
using System.Text;
using eee.Sheffield.PZ.Math;
using System.Drawing;
using System.Drawing.Imaging;

namespace eee.Sheffield.PZ.Imaging.ImageFilter
{
    public class Crop : ImageFilter//, IGraylevelImageFilter
    {
        #region Fields
        private PZPoint _upRightCorner = null;
        private int _newWidth = 0;
        private int _newHeight = 0;
        //private string flag = "[Image -> Crop]";
        #endregion

        #region Properties
        public PZPoint UpRightCorner { get { return _upRightCorner; } set { _upRightCorner = new PZPoint(value); } }
        public int NewWidth { get { return _newWidth; } set { _newWidth = value; } }
        public int NewHeight { get { return _newHeight; } set { _newHeight = value; } }
        //public string Flag() { return flag; }
        //public string Log() { return _log; }
        #endregion

        #region Constructor
        public Crop()
        {
            _flag = "[Image -> Crop]";
        }
        #endregion

        #region Apply filter
        //public Bitmap ApplyGraylevelImageFilter(Bitmap srcImage) { return Apply(srcImage, out _log); }       
        //public PZMath_matrix ApplyDoubleGraylevelImageFilter(PZMath_matrix srcMatrix) { return ApplyDouble(srcMatrix, out _log); }

        //private Bitmap Apply(Bitmap srcImage, out string logText)
        //{
        //    PZMath_matrix srcMatrix = _converter.ConvertGraylevelBitmapToMatrix(srcImage);
        //    PZMath_matrix dstMatrix = ApplyDouble(srcMatrix, out logText);
        //    Bitmap dstImage = _converter.ConvertMatrixToGraylevelBitmap(dstMatrix);
        //    return dstImage;
        //}     

        protected override PZMath_matrix ApplyDoubleGraylevelImageFilter(PZMath_matrix srcMatrix, out string logText)
        {
            if (_upRightCorner == null)
                throw new ApplicationException("Crop::ApplyDouble(), up-right-corner is not set.");
            if (_newHeight == 0)
                throw new ApplicationException("Crop::ApplyDouble(), height of cropped image is not set.");
            if (_newWidth == 0)
                throw new ApplicationException("Crop::ApplyDouble(), width of cropped image is not set.");

            int upRightCornerX = Convert.ToInt16(_upRightCorner.x);
            int upRightCornerY = Convert.ToInt16(_upRightCorner.y);

            // get source
            int width = srcMatrix.ColumnCount;
            int height = srcMatrix.RowCount;

            if (upRightCornerX + _newWidth > width)
                throw new ApplicationException("Crop::ApplyDouble(), width of cropped image out of index.");
            if (upRightCornerY + _newHeight > height)
                throw new ApplicationException("Crop::ApplyDouble(), height of cropped image out of index.");

            PZMath_matrix dstMatrix = srcMatrix.Submatrix(upRightCornerY, upRightCornerX, _newHeight, _newHeight);

            logText = "succeeds.";
            return dstMatrix;
        } // ApplyDoubleGraylevelImageFilter()

        protected override PZMath_matrix[] ApplyDoubleColourImageFilter(PZMath_matrix[] srcMatrixArray, out string logText)
        {
            // TODO: add this function
            
            throw new Exception("The method or operation is not implemented.");
        } // ApplyDoubleColourImageFilter()
        #endregion
    }
}
